package com.spring.dao1;

import com.spring.pojo.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BookDao {
    @Autowired
    JdbcTemplate jdbcTemplate;
    public int addBook(Book book){
        return jdbcTemplate.update("INSERT INTO `test`.`book` (`name`, `author`) VALUES( ?, ?) ",book.getName(),book.getAuthor());
    }

    public int updateBook(Book book){
        return jdbcTemplate.update("UPDATE `test`.`book` SET`name` = ?,`author` = ? WHERE `id` = ? ",book.getName(),book.getAuthor(),book.getId());
    }

    public int deleteBook(Integer id){
        return jdbcTemplate.update("DELETE FROM `test`.`book` WHERE `id` = ?",id);
    }

    public Book getBookById(Integer id){
        return jdbcTemplate.queryForObject("SELECT * FROM `test`.`book` WHERE `id` = ?",
                new BeanPropertyRowMapper<>(Book.class),id);
    }
    public List<Book> getAllBood(){
        return jdbcTemplate.query("SELECT * FROM `test`.`book`", new BeanPropertyRowMapper<>(Book.class));
    }
}
